/**
 * Created by BaiYJ on 2017/4/21.
 */

var editor; // use a global for the submit and return data rendering
var table;
var userTypeId = 0;
var user;
var communityId = "";
$(document).ready(function () {
    $.ajax({
        url: '/commis/api/user/currentUser',
        type: "GET",
        async: false,
        dataType: 'json',
        data: {},
        success: function (response) {
            if (response.status == 0) {
                console.log("用户存在");
                user = response.data;
                userTypeId = user.userTypeId;
                if (userTypeId == 4) {
                    communityId = user.communityId;
                }
                console.log(userTypeId);
            } else if (response.status == 1) {
                alert("登录用户已经过期，请重新登录");
                window.parent.location.href = "../../login.html";
            }
        }, error: function (msg) {
            alert("登录用户已经过期，请重新登录");
            window.parent.location.href = "../../login.html";
        }
    })

    $.ajax({
        url: '/commis/api/community/allCom',
        async: false,
        dataType: 'json',
        success: function (response) {
            console.log(response.length);
            for (var i = 0; i < response.length; i++) {
                $("#community").append("<option value='" + response[i].communityId + "'>" + response[i].communityName + "</option>");
            }


        }, error: function () {

        }
    });


    if (userTypeId == 1) {
        $("#communityDiv").show();

    } else if (userTypeId == 2) {
        $("#communityDiv").show();


    } else if (userTypeId == 3||userTypeId == 6||userTypeId == 7||userTypeId == 8||userTypeId == 9) {
        $("#communityDiv").show();


    } else if (userTypeId == 4) {
        $("#communityDiv").hide();
        $("#addDataDiv").show();

    }
    $('#startTime').datetimepicker({
        language: 'zh-CN',
        format: "yyyy-mm-dd",
        minView: "year",
        autoclose: true,
        // todayBtn: true,

    });
    $('#endTime').datetimepicker({
        language: 'zh-CN',
        format: "yyyy-mm-dd",
        minView: "year",
        autoclose: true,
        // todayBtn: true,

    });
    table = $('#dataTable').DataTable({
        ajax: "/commis/api/performance/pageDatatables",
        autoWidth: true,
        responsive: true,
        serverSide: true,
        processing: true,
        lengthChange: false,//是否允许用户改变表格每页显示的记录数
        ordering: false,//是否允许用户排序
        paging: true,//是否分页
        pagingType: "full_numbers",//除首页、上一页、下一页、末页四个按钮还有页数按钮
        searching: false,//是否开始本地搜索
        language: {
            emptyTable: "没有数据",
            search: "搜索:",
            paginate: {
                first: "首页",
                previous: "上一页",
                next: "下一页",
                last: "尾页"

            },
            lengthMenu: "每页 _MENU_ 条记录",
            zeroRecords: "没有找到记录",
            info: "第 _PAGE_ 页 ( 总共 _PAGES_ 页 )，共 _TOTAL_ 项",
            infoEmpty: "无记录",
            infoFiltered: "(从 _MAX_ 条记录过滤)"
        },
        columns: [
            {
                data: 'person_name', defaultContent: ""

            }, {
                data: 'community_name', defaultContent: ""
            }, {
                data: 'sum_affairs', defaultContent: ""
            }, {
                data: 'sum_sick', defaultContent: ""
            }, {
                data: 'sum_year', defaultContent: ""
            }, {
                data: 'sum_maternity', defaultContent: ""
            }, {
                data: 'sum_marriage', defaultContent: ""
            }, {
                data: 'sum_funeral', defaultContent: ""
            }, {
                data: 'sum_length', defaultContent: ""
            }, {
                data: 'sum_type', defaultContent: ""
            }]

    });


})

$('#search').on('click', function () {

    var url = "/commis/api/performance/pageDatatables?1=1"
    var communityId = $('#community').val();
    url = url + "&communityId=" + communityId;
    if ($("#startTime").val() != "") {
        var startTime = $('#startTime').val();
        url = url + "&startTime=" + startTime;
    }
    if ($("#endTime").val() != "") {
        var endTime = $('#endTime').val();
        url = url + "&endTime=" + endTime;
    }


    table.ajax.url(url).load();
});

$('#export').on('click', function () {

    var url = "/commis/api/performance/export?1=1"
    var communityId = $('#community').val();
    url = url + "&communityId=" + communityId;
    if ($("#startTime").val() != "") {
        var startTime = $('#startTime').val();
        url = url + "&startTime=" + startTime;
    }else{
        alert("开始时间不能为空");
        return false;
    }
    if ($("#endTime").val() != "") {
        var endTime = $('#endTime').val();
        url = url + "&endTime=" + endTime;
    }else{
        alert("结束时间不能为空");
        return false;
    }


    window.parent.location.href = url;
});
//设置框架
$(function () {
    $("#page_style").frame({
        float: 'left',
        menu_nav: '.Quick_operation',
        color_btn: '.skin_select',
        Sellerber_menu: '.list_content',
        Sellerber_header: '.Sellerber_header',
    });
});
$("#iframe_box").niceScroll({
    cursorcolor: "#888888",
    cursoropacitymax: 1,
    touchbehavior: false,
    cursorwidth: "5px",
    cursorborder: "0",
    cursorborderradius: "5px"
});
/*********************/
require.config({
    paths: {
        echarts: './js/dist'
    }
});